<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="1000" top="100px" destroy-on-close
		@closed="$emit('closed')">
		<info ref="info"></info>
		<el-form :model="FormData" class="u-border u-p-15" :rules="rules" :disabled="mode=='show'" ref="dialogForm"
			label-width="110px" label-position="left">
			<el-form-item label="处理记录" prop="describeText">
				<el-input type="textarea" v-model="FormData.describeText" placeholder="请输入处理记录"></el-input>
			</el-form-item>
			<div class="u-flex">
				<el-form-item label="是否处理完成" prop="" class="u-m-r-30">
					<el-checkbox v-model="FormData.finsh" label="" />
				</el-form-item>
				<el-form-item v-show="!FormData.finsh" label="下一个处理人" prop="">
					<Users v-model="FormData.nextUser" placeholder="请选择下一个处理人"></Users>
				</el-form-item>
			</div>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">返 回</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import info from '../components/info'
	export default {
		components: {
			info
		},
		emits: ['success', 'closed'],
		data() {
			return {
				showAMap: false,
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '处理',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				fileurlArr: [],
				//表单数据
				FormData: {
					finsh: false
				},
				//验证规则
				rules: {
					describeText: [{
						required: true,
						message: '请输入处理记录'
					}],
					nextUser: [{
						required: true,
						message: '请选择下一个处理人'
					}],
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					emitPath: false,
					checkStrictly: true
				},
				unitList: [],
				userInfo: {},
				records: []
			}
		},
		created() {
			this.userInfo = this.$TOOL.data.get("USER_INFO")
		},
		mounted() {
			this.getUnit()
		},
		methods: {
			async getUnit() {
				let data = {
					page: 1,
					pagesize: 100
				}
				const res = await this.$API.enterprise.unit.list.post(data)
				if (res.data.datas) {
					this.unitList = res.data.datas
				}
			},
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit() {
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if (!valid) {
					return false
				}
				this.isSaveing = true
				let data = {
					id: this.FormData.id,
					describeText: this.FormData.describeText,
					finsh: this.FormData.finsh
				}
				if (this.FormData.finsh) {
					data.nextUser = this.FormData.nextUser
				}
				const res = await this.$API.fault.hitch.handle.post(data)
				this.isSaveing = false;
				if (res.code == 200) {
					this.$emit('success', this.FormData, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//表单注入数据
			async setData(info) {
				const res = await this.$API.fault.hitch.info.post({
					id: info.id
				})
				const data = res.data
				this.$nextTick(() => {
					this.$refs.info.setData(data)
				})
				this.FormData.id = data.id
				if (data.unit) {
					this.FormData.unit = data.unit.id
				}
				this.FormData.linkName = data.linkName
				this.FormData.linkTel = data.linkTel
				this.FormData.startTime = data.startTime
				this.FormData.endTime = data.endTime
				this.FormData.insure = data.insure
				if (data.files) {
					let files = data.files.split(",")
					const newArray = files.map(item => {
						return {
							name: item,
							url: item
						}
					})
					this.FormData.files = newArray
					console.log(12333, newArray)
				}
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.FormData, data)
			}
		}
	}
</script>

<style>
</style>